package lan.battletank.element;

import lan.battletank.R;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

public class ControlPanelEntity {
	
	private Context mContext;
	
	/** Pixel width and hight of control panel image. Left is the same size as Right */
	private static final int CONTROL_PANEL_WIDTH = 100;
	private static final int CONTROL_PANEL_HEIGHT = 50;
	/** Pixel width and hight of control panel FIRE image. Left is the same size as Right */
	private static final int CONTROL_FIRE_WIDTH = 100;
	private static final int CONTROL_FIRE_HEIGHT = 50;
	/** The margin size to put the control panel from the edge of the screen. */
	private static final int MARGIN = 0;
	
	/** Control move panel image to use */
	private Bitmap mControlLeftImage;
	private Bitmap mControlRightImage;
	private Bitmap mControlLeftHighLightImage;
	private Bitmap mControlRightHighLightImage;
	
	/** Control cannon angle panel image. */
	private Bitmap mControlUpImage;
	private Bitmap mControlDownImage;
	private Bitmap mControlUpHighLightImage;
	private Bitmap mControlDownHighLightImage;
	
	/** Control cannon angle panel image. */
	private Bitmap mControlFireImage;
	private Bitmap mControlFireHighLightImage;
	
	/** The bounds position to draw control move panel. */
	private int cL_l;	// left control: left
	private int cL_t;	// left control: top
	private int cL_r;	// left control: right
	private int cL_b;	// left control: bottom
	private int cR_l;	// right control: left
	private int cR_t;	// right control: top
	private int cR_r;	// right control: right
	private int cR_b;	// right control: bottom

	/** The bounds position to draw control move panel. */
	private int cU_l;	// up control: left
	private int cU_t;	// up control: top
	private int cU_r;	// up control: right
	private int cU_b;	// up control: bottom
	private int cD_l;	// down control: left
	private int cD_t;	// down control: top
	private int cD_r;	// down control: right
	private int cD_b;	// down control: bottom
	
	/** The bounds position to draw control FIRE panel. */
	private int cF_l;	// fire control: left
	private int cF_t;	// fire control: top
	private int cF_r;	// fire control: right
	private int cF_b;	// fire control: bottom
	
    /** The states of the control move panel */
	private boolean mCLeftPressed;
	private boolean mCRightPressed;
	
	/** The states of the control angle panel */
	private boolean mCUpPressed;
	private boolean mCDownPressed;
	
	/** The states of the control FIRE panel */
	private boolean mCFirePressed;
	
	/** Constructor, initial all parameters here. */
	public ControlPanelEntity(Context context) {
		mContext = context;
		Resources res = mContext.getResources();
		// Faster to draw in bitmap.
		mControlLeftImage = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.left), CONTROL_PANEL_WIDTH, CONTROL_PANEL_HEIGHT, true);
		mControlRightImage = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.right), CONTROL_PANEL_WIDTH, CONTROL_PANEL_HEIGHT, true);
		mControlLeftHighLightImage = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.left_highlight), CONTROL_PANEL_WIDTH, CONTROL_PANEL_HEIGHT, true);
		mControlRightHighLightImage = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.right_highlight), CONTROL_PANEL_WIDTH, CONTROL_PANEL_HEIGHT, true);
		
		mControlUpImage = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.up), CONTROL_PANEL_WIDTH, CONTROL_PANEL_HEIGHT, true);
		mControlDownImage = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.down), CONTROL_PANEL_WIDTH, CONTROL_PANEL_HEIGHT, true);
		mControlUpHighLightImage = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.up_highlight), CONTROL_PANEL_WIDTH, CONTROL_PANEL_HEIGHT, true);
		mControlDownHighLightImage = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.down_highlight), CONTROL_PANEL_WIDTH, CONTROL_PANEL_HEIGHT, true);
		
		mControlFireImage = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.fire), CONTROL_FIRE_WIDTH, CONTROL_FIRE_HEIGHT, true);
		mControlFireHighLightImage = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.fire_highlight), CONTROL_FIRE_WIDTH, CONTROL_FIRE_HEIGHT, true);
		
		cL_l = 0;
		cL_t = 0;
		cL_r = 0;
		cL_b = 0;
		cR_l = 0;
		cR_t = 0;
		cR_r = 0;
		cR_b = 0;
		
		cU_l = 0;
		cU_t = 0;
		cU_r = 0;
		cU_b = 0;
		cD_l = 0;
		cD_t = 0;
		cD_r = 0;
		cD_b = 0;
		
		cF_l = 0;
		cF_t = 0;
		cF_r = 0;
		cF_b = 0;
		
		mCLeftPressed = false;
		mCRightPressed = false;
		mCUpPressed = false;
		mCDownPressed = false;
		mCFirePressed = false;
	}
	
	public void draw(Canvas c) {
		if (mCLeftPressed) {
			c.drawBitmap(mControlLeftHighLightImage, cL_l, cL_t, null);
		} else {
			c.drawBitmap(mControlLeftImage, cL_l, cL_t, null);
		}
		if (mCRightPressed) {
			c.drawBitmap(mControlRightHighLightImage, cR_l, cR_t, null);
		} else {
			c.drawBitmap(mControlRightImage, cR_l, cR_t, null);
		}
		if (mCUpPressed) {
			c.drawBitmap(mControlUpHighLightImage, cU_l, cU_t, null);
		} else {
			c.drawBitmap(mControlUpImage, cU_l, cU_t, null);
		}
		if (mCDownPressed) {
			c.drawBitmap(mControlDownHighLightImage, cD_l, cD_t, null);
		} else {
			c.drawBitmap(mControlDownImage, cD_l, cD_t, null);
		}
		if (mCFirePressed) {
			c.drawBitmap(mControlFireHighLightImage, cF_l, cF_t, null);
		} else {
			c.drawBitmap(mControlFireImage, cF_l, cF_t, null);
		}
	}

	
	public boolean touched(TankEntity tank, float touchedX, float touchedY) {
		if (touchedX >= cL_l && touchedX <= cL_r && touchedY >= cL_t && touchedY <= cL_b) {
			mCLeftPressed = true;
			mCRightPressed = false;
			return true;
		} else if (touchedX >= cR_l && touchedX <= cR_r && touchedY >= cR_t && touchedY <= cR_b) {
			mCLeftPressed = false;
			mCRightPressed = true;
			return true;
		} else if (touchedX >= cU_l && touchedX <= cU_r && touchedY >= cU_t && touchedY <= cU_b) {
			mCUpPressed = true;
			mCDownPressed = false;
			return true;
		} else if (touchedX >= cD_l && touchedX <= cD_r && touchedY >= cD_t && touchedY <= cD_b) {
			mCUpPressed = false;
			mCDownPressed = true;
			return true;
		} else if (!tank.isFiring() && touchedX >= cF_l && touchedX <= cF_r && touchedY >= cF_t && touchedY <= cF_b) {
			mCFirePressed = true;
			return true;
		} else {
			mCLeftPressed = false;
			mCRightPressed = false;
			mCUpPressed = false;
			mCDownPressed = false;
			mCFirePressed = false;
		}
		return false;
	}

	/**
	 * If the touch is maintain in the control panel.
	 * @param touchedX
	 * @param touchedY
	 * @return boolean
	 */
	public boolean maintainTouched(float touchedX, float touchedY) {
		if (mCLeftPressed && touchedX >= cL_l && touchedX <= cL_r && touchedY >= cL_t && touchedY <= cL_b) {
			return true;
		} else if (mCRightPressed && touchedX >= cR_l && touchedX <= cR_r && touchedY >= cR_t && touchedY <= cR_b) {
			return true;
		} else if (mCUpPressed && touchedX >= cU_l && touchedX <= cU_r && touchedY >= cU_t && touchedY <= cU_b) {
			return true;
		} else if (mCDownPressed && touchedX >= cD_l && touchedX <= cD_r && touchedY >= cD_t && touchedY <= cD_b) {
			return true;
		} else if (mCFirePressed && touchedX >= cF_l && touchedX <= cF_r && touchedY >= cF_t && touchedY <= cF_b) {
			return true;
		} else {
			mCLeftPressed = false;
			mCRightPressed = false;
			mCUpPressed = false;
			mCDownPressed = false;
			mCFirePressed = false;
		}
		return false;
	}
	
	public void setPositionAndInitialize(int canvasWidth, int canvasHeight) {
		cL_l = MARGIN;
		cL_t = canvasHeight - CONTROL_PANEL_HEIGHT - MARGIN;
		cL_r = MARGIN + CONTROL_PANEL_WIDTH;
		cL_b = canvasHeight - MARGIN;
		cR_l = cL_r + MARGIN;
		cR_t = cL_t;
		cR_r = cR_l + CONTROL_PANEL_WIDTH;
		cR_b = cL_b;
		
		cD_r = canvasWidth - MARGIN;
		cD_l = cD_r - CONTROL_PANEL_WIDTH;
		cD_t = cL_t;
		cD_b = cL_b;
		cU_r = cD_l - MARGIN;
		cU_l = cU_r - CONTROL_PANEL_WIDTH;
		cU_t = cL_t;
		cU_b = cL_b;
		
		cF_l = (canvasWidth - CONTROL_FIRE_WIDTH) / 2;
		cF_t = cL_t;
		cF_r = cF_l + CONTROL_FIRE_WIDTH;
		cF_b = cL_b;
	}
	
	public void reset() {
		mCLeftPressed = false;
		mCRightPressed = false;
		mCUpPressed = false;
		mCDownPressed = false;
		mCFirePressed = false;
	}
	
	public boolean goLeft() {
		return mCLeftPressed;
	}
	
	public boolean goRight() {
		return mCRightPressed;
	}
	
	public boolean angleUp() {
		return mCUpPressed;
	}
	
	public boolean angleDown() {
		return mCDownPressed;
	}
	
	public boolean firePressed() {
		return mCFirePressed;
	}
}
